﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace TradeMo
{
    public partial class ImagePage : PhoneApplicationPage
    {
        private PageOrientation _lastOrientation;

        public ImagePage()
        {
            InitializeComponent();

            OrientationChanged += ImagePage_OrientationChanged;
            _lastOrientation = Orientation;
        }

        void ImagePage_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            PageOrientation newOrientation = e.Orientation;
            RotateTransition transitionElement = new RotateTransition();

            switch (newOrientation)
            {
                case PageOrientation.Landscape:
                case PageOrientation.LandscapeRight:
                    if (_lastOrientation == PageOrientation.PortraitUp)
                        transitionElement.Mode = RotateTransitionMode.In90Counterclockwise;
                    else
                        transitionElement.Mode = RotateTransitionMode.In180Clockwise;
                    break;
                case PageOrientation.LandscapeLeft:
                    if (_lastOrientation == PageOrientation.LandscapeRight)
                        transitionElement.Mode = RotateTransitionMode.In180Counterclockwise;
                    else
                        transitionElement.Mode = RotateTransitionMode.In90Clockwise;
                    break;
                case PageOrientation.Portrait:
                case PageOrientation.PortraitUp:
                    if (_lastOrientation == PageOrientation.LandscapeLeft)
                        transitionElement.Mode = RotateTransitionMode.In90Counterclockwise;
                    else
                        transitionElement.Mode = RotateTransitionMode.In90Clockwise;
                    break;
                default:
                    break;
            }

            // Execute the transition
            var transition = transitionElement.GetTransition(App.AppPage);
            transition.Completed += delegate { transition.Stop(); };
            transition.Begin();
            _lastOrientation = newOrientation;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            string name;

            if (!NavigationContext.QueryString.TryGetValue("picture", out name)) return;
            Picture.Source = new BitmapImage(new Uri(name, UriKind.RelativeOrAbsolute));
        }

        private void Picture_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
            // Scale X & Y must be the same
            double scale = 1.0;
            if ((e.DeltaManipulation.Scale.X != 0) && (e.DeltaManipulation.Scale.X != 1.0))
                scale = e.DeltaManipulation.Scale.X;
            if ((e.DeltaManipulation.Scale.Y != 0) && (e.DeltaManipulation.Scale.Y != 1.0))
            {
                if (e.DeltaManipulation.Scale.Y < scale)
                    scale = e.DeltaManipulation.Scale.Y;
            }

            if (scale != 1.0)
            {
                //if (scale < 0.5) scale = 0.5;
                //if (scale > 3.0) scale = 3.0;

                PictureTransform.ScaleX *= scale;
                PictureTransform.ScaleY *= scale;

                if (PictureTransform.ScaleX < 0.5) PictureTransform.ScaleX = 0.5;
                if (PictureTransform.ScaleY < 0.5) PictureTransform.ScaleY = 0.5;
                if (PictureTransform.ScaleX > 3.0) PictureTransform.ScaleX = 3.0;
                if (PictureTransform.ScaleY > 3.0) PictureTransform.ScaleY = 3.0;
            }

            PictureTransform.TranslateX += e.DeltaManipulation.Translation.X;
            PictureTransform.TranslateY += e.DeltaManipulation.Translation.Y;
        }

    }
}